package co.edu.usbcali.rebio.report;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXlsExporter;

public class ReporteCincoXLS extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ReporteCincoXLS() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Ejecutar(request, response);

	
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Ejecutar(request, response);

		
	}
	
	public void Ejecutar(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {


		try {

			//HashMap<String, Object> Parametros = new HashMap<String, Object>();
			//Parametros.put("NO_FACTURA", new BigDecimal(5252));
			
			//Parametros.put("cli_nombre", request.getParameter("factura"));
			

			String RutaReporte = getServletContext().getRealPath("/Reportes");

			Context initialContext = new InitialContext();
			
			DataSource ds = (DataSource)initialContext.lookup("java:/comp/env/jdbc/Reporte");
			Connection con = ds.getConnection();

			FileInputStream ig = new FileInputStream(RutaReporte + "/Consulta_5.jasper");

			JasperPrint jasper = JasperFillManager.fillReport(ig, null, con);
			response.setContentType("aplication/xls");
			
			response.setHeader("Content-disposition", "inline; filename=\"Reporte Area Total de Haciendas.xls\"");
			//response.setHeader("Content-disposition", "attachment; filename=\"reporte.pdf\"");
			File archivoGenerado = new File(RutaReporte + "/Reporte.xls");
			FileOutputStream fos = new FileOutputStream(archivoGenerado);

			//Generar pdf
			
			JRXlsExporter exporter = new JRXlsExporter();
			//JRPdfExporter exporter = new JRPdfExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasper);

			ServletOutputStream out = response.getOutputStream();
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);

			exporter.exportReport();
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
